//
//  WSTaskItemCell.m
//  CharonTVProject
//
//  Created by karong on 2023/7/31.
//

#import "WSTaskItemCell.h"
#import "WSTask.h"
#import "WSGradientColorButton.h"

@interface WSTaskItemCell ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet WSGradientColorButton *receiveBtn;
@property (weak, nonatomic) IBOutlet UILabel *subTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *minTitleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@end

@implementation WSTaskItemCell
- (void)setDataWithObject:(WSTask *)object {
    self.titleLabel.text = object.charonTV_title;
    self.subTitleLabel.text = WSSF(@"+%ldCoins", (long)object.charonTV_coins);
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:object.charonTV_thumb]];
    [self setMinTitleLabelTextWithObject:object];
    [self setReceiveBtnStatusWithObject:object];
}

- (void)setMinTitleLabelTextWithObject:(WSTask *)object {
    BOOL show = NO;
    NSString * text = @"";
    if (object.charonTV_is_show_progress == 1 || object.charonTV_times > 1 || object.charonTV_task_identify == 8 || object.charonTV_task_identify == 9 || object.charonTV_task_identify == 11) {
        show = YES;
    }
    if (show) {
        if (object.charonTV_total_times > 0) {
            text = WSSF(@"%ld/%ld%@", object.charonTV_over_times, object.charonTV_total_times, object.charonTV_unit_name);
        }
        else if (object.charonTV_task_identify == 9) {
            text = WSSF(@"%ld/%ld%@", object.charonTV_over_times, object.charonTV_unit_num, object.charonTV_unit_name);
        }
        else {
            text = WSSF(@"%ld/%ld", object.charonTV_over_times, object.charonTV_total_times);
        }
        self.minTitleLabel.text = text;
    }
    self.minTitleLabel.hidden = !show;
}

- (void)setReceiveBtnStatusWithObject:(WSTask *)object {
    ///任務狀態（0未完成，1待領取，2已領取）
    UIColor * textColor = UIColor.clearColor;
    UIColor * borderColor = UIColor.clearColor;
    UIColor * color1 = UIColor.clearColor;
    UIColor * color2 = UIColor.clearColor;
    NSString * text = @"";
    BOOL enable = YES;
    switch (object.charonTV_task_status) {
        case 0:
            textColor = UIColor.font_red_F23735;
            borderColor = UIColor.font_red_F23735;
            text = APPLocalized(@"去完成");
            break;
        case 1:
            textColor = UIColor.font_white_ffffff;
            color1 = [UIColor colorFromHexRGB:@"DD2D08"];
            color2 = [UIColor colorFromHexRGB:@"F9661A"];
            text = APPLocalized(@"領取");
            break;
        case 2:
            textColor = UIColor.font_white_ffffff;
            color1 = [UIColor colorFromHexRGB:@"cecece"];
            color2 = [UIColor colorFromHexRGB:@"cecece"];
            text = APPLocalized(@"已完成");
            enable = NO;
            break;
        default:
            break;
    }
    if (object.charonTV_task_identify == 0) {
        enable = YES;
    }
    self.receiveBtn.tag = object.charonTV_jump_type;
    self.receiveBtn.userInteractionEnabled = enable;
    self.receiveBtn.layer.borderColor = borderColor.CGColor;
    [self.receiveBtn setTitleColor:textColor forState:(UIControlStateNormal)];
    [self.receiveBtn setTitle:text forState:(UIControlStateNormal)];
    
    [self.receiveBtn resetCorlor1:color1 color2:color2];

}

- (IBAction)receiveAction:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(taskcharonTV_ItemCellAction:type:)]) {
        [self.delegate taskcharonTV_ItemCellAction:self type:sender.tag];
    }
}
@end
